﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace DXApplication1月报表
{
    public partial class XtraFormMES_U8项目号对照 : DevExpress.XtraEditors.XtraForm
    {
        public XtraFormMES_U8项目号对照()
        {
            InitializeComponent();
        }

       

      

        private void simpleButton查询_Click(object sender, EventArgs e)
        {

            查询();

        }

        private void 查询()
        {
            string sql = "select * from [Z_MES_U8项目号对照] order by id,MES项目号";
            DataTable dt = SqlHelper.SqlHelper.ExecuteDataset(sql).Tables[0];
            this.gridControl1.DataSource = dt;
        }

        private void simpleButton保存_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textEditMES项目号.Text.Trim()) == true)
            {
                XtraMessageBox.Show("请输入MES项目号");
                return;
            }

            if (string.IsNullOrEmpty(memoEditU8项目号.Text.Trim()) == true)
            {
                XtraMessageBox.Show("请输入U8项目号");
                return;
            }

            string[] U8项目号s = memoEditU8项目号.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            if (U8项目号s.Length == 0)
            {
                MessageBox.Show("请输入U8项目号,多个U8项目号请分多行");
                return;
            }
            else
            {
                string sql = "";

                foreach (string item in U8项目号s)
                {
                    sql += $@"insert into Z_MES_U8项目号对照 ([MES项目号],[U8项目号],[已确定]) 
                        values ('{textEditMES项目号.Text.Trim()}','{item}','{checkEdit已确定.Checked}')";
                }
              int r =  SqlHelper.SqlHelper.ExecuteNonQuery(sql);
                XtraMessageBox.Show($"成功插入{r}行");
                查询();
            }


          
        }



        private void gridView1_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName == "已确定")  // 你的复选框列名
            {
                bool newValue = Convert.ToBoolean(e.Value);
                
                DataRow currentRow = gridView1.GetDataRow(e.RowHandle);
                string s = currentRow["已确定"].ToString();
                string id = currentRow["id"].ToString();
                string sql = $"update Z_MES_U8项目号对照 set 已确定 = '{newValue}' where id = {id}";
                SqlHelper.SqlHelper.ExecuteNonQuery(sql);
                // 如果要立即更新当前行的DataTable
                //gridView1.SetRowCellValue(e.RowHandle, e.Column, newValue);
                查询();
            }
        }
        private void repositoryItemButtonEdit删除_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            int rowHandle = gridView1.FocusedRowHandle;
            if (rowHandle >= 0)
            {
                DataRow currentRow = gridView1.GetDataRow(rowHandle);
                string id = currentRow["id"].ToString();
                string sql = $"delete Z_MES_U8项目号对照  where id = {id}";
                SqlHelper.SqlHelper.ExecuteNonQuery(sql);
                // 执行删除
                查询();
            }
        }
        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            //DataRow currentRow = gridView1.GetDataRow(e.RowHandle);
            //if (currentRow != null)
            //{
            //    // 比如修改某个字段
            //    string s = currentRow["已确定"].ToString();
            //    string id = currentRow["id"].ToString();
            //    // 如果需要强制回写到 DataTable 并触发数据同步
            //    currentRow.AcceptChanges();  // 可选，看你是否要标记已提交
            //}
        }
    }
}